package model

import (
	"gorm.io/gorm"
)

type ProblemBasic struct {
	gorm.Model
	Identity   string           `gorm:"column:identity;type:varchar(36);index" json:"identity"`
	Categories []*CategoryBasic `gorm:"many2many:problem_category;foreignKey:Identity;references:Identity;joinForeignKey:ProblemId;joinReferences:CategoryId"`
	Title      string           `gorm:"column:title;type:varchar(255)" json:"title"`
	Content    string           `gorm:"column:content;type:text" json:"content"`
	MaxRuntime uint             `gorm:"column:max_runtime;type:int(11)" json:"max_runtime"`
	MaxMem     uint             `gorm:"column:max_mem;type:int(11)" json:"max_mem"`
}

func (table *ProblemBasic) TableName() string {
	return "problem_basic"
}

func GetProblemList(keyword string, category_identity string) *gorm.DB {
	tx := DB.Debug().Model(&ProblemBasic{}).Preload("Categories").
		Where("title like ? OR content like ?", "%"+keyword+"%", "%"+keyword+"%")
	if category_identity != "" {
		tx.Joins("INNER JOIN problem_category pc ON pc.problem_id = problem_basic.identity").
			Where("pc.category_id = ?", category_identity)
	}
	return tx
}
